package agents;

import gui.FactoryGui;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.util.HashMap;
import java.util.Map;

import agentInterface.AGVDetails;
import agentInterface.MachineDetails;
import agentInterface.PowerDetails;

public class FactoryAgent extends GuiAgent{
	private static final long serialVersionUID = 1L;

	private FactoryGui myGui;
	
	//meter o plan do agv no agv details
	//meter o isprocessing no machinedetails
	HashMap<String,AGVDetails> agvsDetails = new HashMap<String,AGVDetails>();
	
	HashMap<String,MachineDetails> machinesDetails = new HashMap<String,MachineDetails>();
	
	HashMap<String,PowerDetails> powerDetails = new HashMap<String,PowerDetails>();
	
	protected void setup() {
	  	System.out.println(getLocalName() + ": Hello World!");
	  	
	  	registerOnDF();
	  	
	  	initGui();
	  	
	  	receiveDetails();
	  	
	  	updateTables();
	}
	
	private void updateTables()
	{
		addBehaviour(new TickerBehaviour(this, 10) {
			
			@Override
			protected void onTick() {
				myGui.updateAgvTableValues();
				myGui.updateMachineTableValues();
				
			}
		});
	}
	
	private void receiveDetails() {
		addBehaviour(new CyclicBehaviour(this) {
			private static final long serialVersionUID = 1L;

			@Override
			public void action() {
				ACLMessage msg = receive(MessageTemplate.MatchPerformative(ACLMessage.INFORM));
				if (msg != null) {
					
					String sender = msg.getSender().getLocalName();
					if (agvsDetails.containsKey(sender))
					{
						AGVDetails details = null;
						try {
							details = (AGVDetails) msg.getContentObject();
						} catch (UnreadableException e) {
							e.printStackTrace();
						}
						agvsDetails.put(sender, details);
						

						myGui.repaint();
					}
					if (machinesDetails.containsKey(sender))
					{
						MachineDetails details = null;
						try {
							details = (MachineDetails) msg.getContentObject();
						} catch (UnreadableException e) {
							e.printStackTrace();
						}
						machinesDetails.put(sender, details);
						

						myGui.repaint();
					}
					if (powerDetails.containsKey(sender))
					{
						PowerDetails details = null;
						try {
							details = (PowerDetails) msg.getContentObject();
						} catch (UnreadableException e) {
							e.printStackTrace();
						}
						powerDetails.put(sender, details);
						

						myGui.repaint();
					}
					
				}
			}
		});
		
	}

	private void registerOnDF() {
		//registar no DF
				DFAgentDescription dfd = new DFAgentDescription();
				
				ServiceDescription sd1 = new ServiceDescription();
				sd1.setName(getName());
				sd1.setType("factory");

				
				dfd.setName(getAID());
				dfd.addServices(sd1);

				try {
					DFService.register(this, dfd);
				} catch (FIPAException e) {
					System.out.println(getLocalName() + "Cannot register with DF");
					doDelete();
				}
		
	}
	
	private void initGui() {
		myGui = new FactoryGui(this);
		myGui.showGui();
	}

	@Override
	protected void onGuiEvent(GuiEvent arg0) {
	}
	
	public void setAgvDetails (String aid, AGVDetails det){
		agvsDetails.put(aid, det);
	}
	
	public void setMachineDetails (String aid, MachineDetails det){
		machinesDetails.put(aid, det);
	}

	public Map<String, MachineDetails> getMachinesDetails() {
		return machinesDetails;
	}

	public Map<String, AGVDetails> getAgvsDetails() {

		return agvsDetails;
	}

	public void setPowerDetails(String aid, PowerDetails det) {
		powerDetails.put(aid,det);
		
	}

	public Map<String, PowerDetails> getPowerDetails() {
		return powerDetails;
	}
	
	} 
